<template>
  <a-modal
    v-model:visible="show"
    :title="title"
    width="600px"
    height="600px"
    class="dispatchContainer"
    :confirm-loading="confirmLoading"
    @ok="ok"
  >
    <div ref="content" class="mx-auto">
      <DynamicForm ref="dyForm" :model="model" :model-form-setting="modelFormSetting" :force-column="true" />
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
import { useI18n } from "/@/locales/useI18n";
import { ref, shallowRef, watch } from "vue";
import type { DynamicFormExpose, FormItemData } from "/@/components/CURDDialog/models";
import { FormAddItem } from "/@/api/aiot/message/models";
import { DispatchWorkOrderItem, ServiceOrderItem } from "/@/api/aiot/serviceApply/models";
import { dispatchWorkOrders, reassignmentWorkOrders, turnDispatchWorkOrders } from "/@/api/aiot/serviceWorkOrder";
import { message } from "ant-design-vue";
import { BtnType } from "/@/api/aiot/serviceWorkOrder/models";
import { getServiceOrderById } from "/@/api/aiot/serviceApply";

const { t } = useI18n();
const props = defineProps<{
  workOrderId?: string;
  currentBtnType?: string;
  visible: boolean;
  model: DispatchWorkOrderItem;
  modelFormSetting: FormItemData[];
  serviceOrderId?: string;
}>();
const emits = defineEmits(["create", "update:visible"]);
const show = ref<boolean>(false);
const serviceOrderDetails = ref<ServiceOrderItem>({});
watch(
  () => props.visible,
  async (v) => {
    show.value = v;
    if (v && props.currentBtnType === BtnType.DISPATCH && props.serviceOrderId) {
      serviceOrderDetails.value = (await getServiceOrderById({ serviceOrderId: props.serviceOrderId || "" })) || {};
      title.value = t("aiot.serviceOrder.dispatchWorkOrder");
    }
    if (!v) {
      reset();
    }
  }
);
const title = shallowRef<string>("");
watch(
  () => props.currentBtnType,
  () => {
    if (props.currentBtnType === BtnType.REASSIGNMENT) {
      title.value = t("aiot.serviceOrder.changeDispatch");
    } else if (props.currentBtnType === BtnType.DISPATCH) {
      title.value = t("aiot.serviceOrder.dispatchWorkOrder");
    } else if (props.currentBtnType === BtnType.TURNDISPATCH) {
      title.value = t("aiot.serviceOrder.transWorkOrder");
    }
  }
);
//第一次打开
const dyForm = ref<DynamicFormExpose>();
let firstShow = true;
watch(show, (v) => {
  emits("update:visible", v);
  if (firstShow) {
    firstShow = false;
  }
});
function addUser() {}
const selUserIds = ref<string[]>([]);
//窗口关闭
function cancel() {
  emits("update:visible", false);
  reset();
}
//
function reset() {
  if (dyForm.value) {
    dyForm.value.resetFields();
  }
  selUserIds.value = [];
}
const content = ref<HTMLDivElement>();
const confirmLoading = ref<boolean>(false);
function ok(isContinue) {
  if (!dyForm.value) return;
  dyForm.value
    .validate()
    .then(async (data: FormAddItem) => {
      let obj: DispatchWorkOrderItem = { workOrderId: props.workOrderId };
      Object.assign(obj, props.model);
      let req: boolean | null = false;
      confirmLoading.value = true;
      if (props.currentBtnType === BtnType.REASSIGNMENT) {
        req = await reassignmentWorkOrders(obj);
      } else if (props.currentBtnType === BtnType.DISPATCH && !props.serviceOrderId) {
        req = await dispatchWorkOrders(obj);
      } else if (props.currentBtnType === BtnType.TURNDISPATCH) {
        req = await turnDispatchWorkOrders(obj);
      } else if (props.currentBtnType === BtnType.DISPATCH && props.serviceOrderId) {
        obj.workOrdersSaveRequest = serviceOrderDetails.value;
        req = await dispatchWorkOrders(obj);
      }
      confirmLoading.value = false;
      if (req) {
        emits("update:visible", false);
        message.success(t("aiot.serviceOrderOperationMsg.dispatchOkMsg", { name: title.value }));
        emits("create");
      }
    })
    .catch(() => {
      if (content.value) {
        (content.value.parentNode as HTMLDivElement).scrollTop = 0;
      }
    });
}
</script>

<style lang="less" scoped>
:deep(.ant-picker) {
  width: 100%;
}
</style>
